<?php
error_reporting(0);
//header('Content-Type:text/plain;charset=utf-8');
header('Access-Control-Allow-Origin:*');


function getCurrentCount($data) {
	if ($data['IntervalTime'] > 0){
		if ($data['hasNum'] > $data['maxNum']) {
			$decNum = $data['hasNum'] - $data['maxNum'];
			
			$atTime = (time() - $data['atTime']) / $data['IntervalTime'];
			$atTime = ($atTime > 1)? 1: $atTime;
			$counter = floor($data['hasNum'] - $atTime  * $decNum);
		} elseif ($data['maxNum'] > $data['hasNum']) {
			$addNum = $data['maxNum'] - $data['hasNum'];
			
			$atTime = (time() - $data['atTime']) / $data['IntervalTime'];
			$atTime = ($atTime > 1)? 1: $atTime;
			$counter = $data['hasNum'] + floor($atTime  * $addNum);
		}
	} else {
		$counter = $data['hasNum'];
	}
	
	return $counter;
}


function getOnLineNum($fname){
    $arr = explode("/",$_SERVER["PHP_SELF"]);//获取当前文件的位置信息并以“/”分割存入数组中  
    $datfile = './cache/'.$fname.".dat";//设置文件位置  
    if(!file_exists($datfile)){//如果文件不存在  
    	$counter = 1;
    	$data = array(
			'maxNum' => 10000,
			'hasNum' => 1,
			'IntervalTime' => 10 * 60,	//单位秒
			'atTime' => time(),
		);
    
        $cf = fopen($datfile,"w");//建立计数器文件  
        fputs($cf, json_encode($data));//写入初始化值  
        fclose($cf);//关闭文件
    }else{//文件存在则取回计数器里的数据  
        $cf = fopen($datfile,"r");
        $data = trim(fgets($cf)); //读取一行
        $data = json_decode($data, true);
		
		//获取数量
		$counter = getCurrentCount($data);

	    fclose($cf);  
    }
	
	if ($counter <= 0) {
		$counter = 1;
	}

    return $counter;  
}

function setMaxNum($fname, $data){
	$data['atTime'] = time();

    $arr = explode("/",$_SERVER["PHP_SELF"]);//获取当前文件的位置信息并以“/”分割存入数组中  
    $datfile = './cache/'.$fname.".dat";//设置文件位置  
    if(!file_exists($datfile)){//如果文件不存在
        $cf = fopen($datfile, "w");//建立计数器文件  
        fputs($cf, json_encode($data));//写入初始化值  
        fclose($cf);//关闭文件  
    }else{//文件存在

	    $cf = fopen($datfile,"w");
	    fputs($cf, json_encode($data));//写data值  
	    fclose($cf);  
    }  
}

function getConfigNum($fname){
    $arr = explode("/",$_SERVER["PHP_SELF"]);//获取当前文件的位置信息并以“/”分割存入数组中  
    $datfile = './cache/'.$fname.".dat";//设置文件位置  
    $cf = fopen($datfile,"r");
    $data = trim(fgets($cf)); //读取一行
    return json_decode($data, true);
}

$edit = $_GET['edit'];
if ($edit <> null) {
	$submitnum = $_GET['submitnum'];
	if (!empty($submitnum)) {		
    	$data = array(
			'maxNum' => $_GET['maxNum'],
			'IntervalTime' => $_GET['itime'] * 60,	//单位秒	
			'hasNum' => $_GET['hasNum'],
		);

		setMaxNum('olnum', $data);
		
		header("location: ?edit=1");
	} else {
		$data = getConfigNum('olnum');
	}

	$html = "<html>
			<head>
				<title>PHP Test</title>
				<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
			</head>
				<body>
					<form method='get'>
						<div class='row'>
							<label>当前输入人数:".getCurrentCount($data)."</label>
						</div>
						<div class='row'>
							<label>已有人数</label>
							<input type='text' name='hasNum' value='".$data['hasNum']."' />
						</div>
						<div class='row'>
							<label>目标人数</label>
							<input type='text' name='maxNum' value='".$data['maxNum']."' />
						</div>
						<div class='row'>
							<label>更新时间(分钟)</label>
							<input type='text' name='itime' value='". floor($data['IntervalTime'] / 60)."' />
						</div>
						<input type='hidden' name='edit' value='1'/>
						<input type='submit' name='submitnum' value='提交'/>
					</form>
				</body>
			</html>";
			
	echo $html;
} else {
	$counter = getOnLineNum('olnum');
	echo json_encode(array('code'=>200, 'data'=>$counter));
}

?>